package com.jackrain.nea.omslocal.service;
/**
 * Created by Administrator on 2017/4/20.
 */

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: 将消费报错的具体数据保存到客户端本地数据库中,
 * 保存每条消费信息
 * Created with IntelliJ IDEA.
 * User: cs
 * Date: 2017/4/20
 * Time: 18:49
 */
@Component
@Slf4j
public class LocalLogService {


    @Autowired
    private PropertiesConf pconf;

    /**
     * 保存本地错误日志表
     */
    public void insertLog(String errMsg, JSONObject jsonObject, Long pid, String topic, String msgkey) {
        // plateid 平台 id

        try {
            // 获取日志表 sqe
            String squnceSql = "select get_wsequences('T_LOGINFO') as SEQ from dual";
            int id = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));
            String sql = "insert into T_LOGINFO (id,errmsg,plateid,topic,msgkey,orderstatus) values(?,?,?,?,?,?)";
            int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, errMsg, pid, topic, msgkey, jsonObject.getString("status")});

        } catch (Exception e) {
            // 保存错误日志出现异常, 将异常保存进日志文件
            log.debug("topic :" + topic + " msgkey: " + msgkey + "pid" + pid + " errmsg " + errMsg);

        }


    }

    /**
     * 保存本地日志表-京东
     */
    public void insertLogForJd(String errMsg,String pid, String topic, String msgkey,String sellerNick,String orderStatus,int isSuccess) {
        // plateid 平台 id

        try {
            // 获取日志表 sqe
            String squnceSql = "select get_wsequences('t_oms_jdmsglogdetail') as SEQ from dual";
            int id = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));
            String sql = "insert into t_oms_jdmsglogdetail (id,errmsg,plateid,topic,msgkey,sellernick,orderstatus,issuccess) values(?,?,?,?,?,?,?,?)";
            int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, errMsg, pid, topic, msgkey,sellerNick, orderStatus,isSuccess});
        } catch (Exception e) {
            // 保存错误日志出现异常, 将异常保存进日志文件
            log.debug("topic :" + topic + " msgkey: " + msgkey + "pid" + pid + " errmsg " + errMsg);

        }


    }

    /**
     * 保存消费的具体信息
     *
     * @param jsonObject
     * @param pid
     * @param type
     */
    public void insertLogDeatail(JSONObject jsonObject, Long pid, int type, String topic, String msgkey) {
        // plateid 平台 id
        try {

            // 获取日志表 sqe
            String squnceSql = "select get_wsequences('t_loginfordeatail') as SEQ from dual";
            int id = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));

            String sql = "insert into T_LOGINFORDEATAIL (id,plateid,type,topic,msgkey,orderstatus) values(?,?,?,?,?,?)";

            if (topic.equals(pconf.getProperty("ons.itemTopic"))) {
                // 商品没有状态
                int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, pid, type, topic, msgkey, jsonObject.getString("approve_status")});

            }else {
                int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, pid, type, topic, msgkey, jsonObject.getString("status")});

            }
        } catch (Exception e) {
            log.error("保存日志表出现错误" + e.toString());
        }
    }



    /**
     * 保存发送库存消息的日志
     */
    public void insertLogQty(String errmsg, String tag, String topic, String msgkey) {
        // plateid 平台 id
        try {
            // 获取日志表 sqe
            String squnceSql = "select get_wsequences('T_LOGINFOQTY') as SEQ from dual";
            int id = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));
            String sql = "insert into T_LOGINFOQTY ( id,errmsg,tag,msgkey ) values(?,?,?,?)";
            int code = QueryEngine.getInstance().executeUpdate(sql, new Object[]{id, errmsg, tag, msgkey});
        } catch (Exception e) {
            log.error("保存报错日志出现异常" + e.getMessage());

        }

    }

}
